//
//  SLCADemoController.m
//  Objective-C
//
//  Created by 陆承东 on 2018/6/28.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLCADemoController.h"

//#import "AppDelegate.h"
//
//#import "UIDevice+SLCategory.h"

#import "SLCADemoViewModel.h"

@interface SLCADemoController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) SLCADemoViewModel *viewModel;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation SLCADemoController

//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//
//    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    //允许转成横屏
//    appDelegate.allowRotation = YES;
//    //调用横屏代码
//    [UIDevice switchNewOrientation:UIInterfaceOrientationLandscapeRight];
//}
//
//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    //允许转成横屏
//    appDelegate.allowRotation = false;
//    //调用横屏代码
//    [UIDevice switchNewOrientation:UIInterfaceOrientationPortrait];
//}

- (void)initNavBar {
    self.navTitle = @"CoreAnimation";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initData];
    
    [self initView];
}

- (void)initData {
    self.viewModel = [[SLCADemoViewModel alloc] init];
}

- (void)initView {
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    UIView *tableFooterView = [[UIView alloc] init];
    self.tableView.tableFooterView = tableFooterView;
}

#pragma mark ----- UITableView代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.viewModel numberOfSections];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.viewModel numberOfRowsInSection:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SLCADemoModelItem *item = [self.viewModel dataForRowAtIndexPath:indexPath];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"];
    }
    
    if (item) {
        cell.textLabel.text = item.title;
        cell.detailTextLabel.text = item.subTitle;
    }
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *sectionName = [self.viewModel dataForSection:section];
    
    sectionName = sectionName ?: @"";
    
    return sectionName;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    
    SLCADemoModelItem *item = [self.viewModel dataForRowAtIndexPath:indexPath];
    if (item && item.routeK) {
        [[SLVDNSManager shareInstance] openView:item.routeK v:item.routeV];
    }
}

@end


